package com.taobao.snowsnow.hibernate.temp;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.taobao.snowsnow.hibernate.pojo.Category;
import com.taobao.snowsnow.hibernate.pojo.Color;
import com.taobao.snowsnow.hibernate.pojo.Dimension;
import com.taobao.snowsnow.hibernate.pojo.Type;
import com.taobao.snowsnow.hibernate.service.CategoryService;
import com.taobao.snowsnow.hibernate.service.TypeService;

public class Test {
	
	public void aa(String fileName) throws IOException {
//		BufferedReader reader = readFile(fileName); 
//		
//		String tempString = null;  
//		
//		while ((tempString = reader.readLine()) != null) {
//			String address[] = tempString.split(";");
//			String tmp = "[" + address[1] + "]"+"[" + address[2] + "]"+ "[" + address[3] + "]";
//			String t = address[4];
//			
//			
//			System.out.println(tmp);
//			System.out.println(address[4]);
//			System.out.println(filterAddress(t, new String[]{address[1], address[2], address[3]}));
//			System.out.println();
//		}
		String[] str = {"广东省", "广州市", "白云区"};
		System.out.println(filterAddress("广东省广州市白云区新市萧岗花园中街16巷17号档口", str));
	}
	
	/**
	 * <p>过滤地址,有些地址多写了省市区,在此可以把多余的信息过滤掉
	 * @param address
	 * 			要过滤的地址
	 * @param filterString
	 * 			地址内可能包含的省市区信息,数组格式: [省][市][区]
	 * @return
	 */
	private String filterAddress(String address, String[] filterString) {
		
		for(int i = filterString.length - 1; i > 0; i--) {
			if(address.contains(filterString[i])) {
				int length = 0;
				for(String str : filterString) {
					if(str != null)
						length += str.length();
				}
				address = filterAddressString(address, filterString[i], length);
				break;
			}
		}
		
		return address;
	}
	
	/**
	 * @param address
	 * 			待过滤的字符串
	 * @param filterString
	 * 			需要过滤的字符串
	 * @return
	 * 			过滤好的地址
	 */
	private String filterAddressString(String address, String filterString, int length) {
		int index = address.indexOf(filterString);
		
		if(index == -1)
			return address;
		
		// 只匹配句首的filterString
		if(index < length + 1) {
			return address.substring(index + filterString.length());
		}
		
		return address;
	}
	
	public void cc() {
		String reg="^.*(?<!(不合谐))$";//用到了后顾  
		System.out.println(getValue("不管信不信,反正现在非常合谐", "^.*(?<!(不合谐))$"));
        System.out.println("不管信不信,反正现在很不合谐".matches(reg));//false不通过  
        System.out.println("不管信不信,反正现在非常合谐".matches(reg));//true通过  
        System.out.println("不合谐在某国是普遍存在的".matches(reg));//true通过  
	}
	
	/**
	 * 读取文件
	 * @param fileName
	 * 			文件名称
	 * @return
	 *			返回 BufferReader
	 * @throws IOException
	 */
	private BufferedReader readFile(String fileName) throws IOException {
		InputStreamReader irs = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
		return new BufferedReader(irs);
	}
	
	/**
	 * <p>
	 * 根据正则式返回找到的字符串
	 * @param str 
	 * 			要搜索的字符串
	 * @param regEx
	 * 			正则表达式
	 * @return
	 * 			匹配到的字符串
	 */
	private static String getValue(String str, String regEx) {
		Matcher m = null;
		
		Pattern p = Pattern.compile(regEx);
		m = p.matcher(str);
		
		if(m.find()) {
			return str.substring(m.start(), m.end());
		}
		return null;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			new Test().aa("d:/address.txt");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
